function [Tsci2crtbp,Tcrtbp2sci,S_eci] = CRTBP2ECI_se(JD_interest,S_crtbp,JD,Res,Ves)

GM_sun=1.327124400*10^(11);     %Sun
GM_earth=3.986004418*10^(5);    %Earth
% GM_moon = 4.9048695*10^(3);     %Moon
%[km^3/s^2] Gravitational Parameters

r12 = 1.4959965e+8;
%[km]Distance between the Earth and the Moon.

Wcm = sqrt((GM_earth+GM_sun)/r12^3)*[0;0;1];
%[] angular velocity in Cr3bp frame

TU = 5.0226757e+6;
LU = r12;
VU = LU/TU;
%[] Canonical unit conversions


S_crtbp = [S_crtbp(1:3)*LU;S_crtbp(4:6)*VU];
R_crtbp = S_crtbp(1:3);
V_crtbp = S_crtbp(4:6);
%[] Non-inertial position and velocity of the spacecraft at JD_interest.

options=odeset('RelTol',1e-9);
%[] Increase the Ode 45 tol
%% Using Julian date, determine Earth's state at the time of interest.

JD_difference_vector = JD-JD_interest;
%[] Determine the difference time vector

ind = find(JD_difference_vector > 0 , 1 , 'first') - 1;
%[] Find the index of most close previous state

Res_o = Res(:,ind);
Ves_o = Ves(:,ind);
Ses_o = [Res_o;Ves_o];
%[km,km/s] Position and velocity of the Earth WRT Sun.

UTC_JD_interest = CalendarDate(JD_interest);
UTC_JD_closest = CalendarDate(JD(ind));
%[] Get UTC time of JD time of interest, which is the JD_interest and
%JD(ind), which is the closest previous information.

time_difference_seconds = etime(UTC_JD_interest,UTC_JD_closest);
%[] Determine time difference in seconds.

if time_difference_seconds == 0
    Res_interest = Res_o;
    Ves_interest = Ves_o;
    Ses_interest = [Res_interest;Ves_interest];
else
    [~,S] = ode45(@(t,S)R2bpCartesianModel(t,S,GM_sun),[0,time_difference_seconds],Ses_o,options);
    S = S';
    %[] Integrate for difference amount to determine exact state of the moon
    %wrt Earth in ECI frame.

    Res_interest = S(1:3,end);
    Ves_interest = S(4:6,end);
    Ses_interest = [Res_interest;Ves_interest];
    %[] Save position velocity and state vector. 
end

%% Determine States of the barycenter of Sun and Earth.

Scms = (GM_earth*Ses_interest)/(GM_sun+GM_earth);
Rcms = Scms(1:3);
% Vcms = Scms(4:6);
%[] Determine the state of the center of mass at JD of interest
%There are no additional terms because the Earth's position wrt Earth is
%zero vector and the gravitational parameter of the satellite is assumed to
%be zero.

%% Make all states WRT CM

Ssuncm = -Scms;
%[] state of the sun WRT cm

Searthcm = Ses_interest-Scms;
Rearthcm_sci = Searthcm(1:3);
Vearthcm_sci = Searthcm(4:6);
%[] state of the earth wrt cm

Teci2sci = ECI2SCI(2021);
Tsci2eci = transpose(Teci2sci);
%[] Transformation matrix from ECI 2 SCI in year 2021;

% Rsate_sci = Teci2sci * S_eci(1:3);
% Vsate_sci = Teci2sci * S_eci(4:6);
%[] position and velocity of the satellite WRT earth in SCI frame

% Rsats_sci = Rsate_sci + Res_interest;
% Vsats_sci = Vsate_sci + Ves_interest;
% Ssats_sci = [Rsats_sci;Vsats_sci];
%[] position velocity and state of the spacecraft WRT SUN in SCI frame.

% Ssatcm = Ssats_sci - Scms;
% Rsatcm = Ssatcm(1:3);
% Vsatcm = Ssatcm(4:6);
%[] Position and velocity of the spacecraft wrt CM of barycenter of Sun-Earth system

%% 4: Determine Teci2crtbp and Tcrtbp2eci

Hm = cross(Res_interest,Ves_interest);
%[1/s]Current specific angular momentum of the Moon WRT the CM in ECI coordinates.

Rhat = Res_interest / norm(Res_interest);
%[]Current CRTBP radial direction in ECI coordinates.

Nhat = Hm / norm(Hm);
%[]Current CRTBP normal direction in ECI coordinates.

That = cross(Nhat,Rhat);
%[]Current CRTBP tangential direction in ECI coordinates.

Tcrtbp2sci = [Rhat, That, Nhat];
%[]Matrix that transforms vectors from CRTBP to ECI coordinates.

Tsci2crtbp = transpose(Tcrtbp2sci);
%[] Matrix that transforms vectors from ECI to CRTBP coordinates.

%% 5: Determine S_crtbp

Rsatcm_sci = Tcrtbp2sci * R_crtbp;
%[] Position of the spacecraft wrt CM in SCI frame

Rsate_sci = Rsatcm_sci - Rearthcm_sci;
%[] Position of the spacecraft wrt earth in SCI frame.

Rsate_eci = Tsci2eci * Rsate_sci;
%[] State of the satellite wrt earth in ECI frame.

Vsatcm_sci = Tcrtbp2sci*(V_crtbp+cross(Wcm,Tsci2crtbp*Rcms)+cross(Wcm,R_crtbp));
%[] velocity of the spacecraft wrt CM in SCI frame.

Vsate_sci = Vsatcm_sci - Vearthcm_sci;
%[] Position of the spacecraft wrt Earth in SCi frame.

Vsate_eci = Tsci2eci * Vsate_sci;
%[] velocity of the spacecraft wrt Earth in ECi frame.

S_eci = [Rsate_eci;Vsate_eci];
%[] state vector of the spacecraft wrt earth in ECI frame.



end